
use "$jfqa_rep/20_Stock_Setup.dta", clear

qui{
drop if missing(convexity_split)
drop if missing(futret)

drop if year<1963
drop if year==1963 & month<6

drop if year>2022
drop if year==2022 & month==12

*convexity split rank
gen convex_five=.
sort monthyear convexity_split
by monthyear: replace convex_five=1 if _n<_N/5
by monthyear: replace convex_five=2 if _n>=_N/5 & _n<_N/5*2
by monthyear: replace convex_five=3 if _n>=_N/5*2 & _n<_N/5*3 
by monthyear: replace convex_five=4 if _n>=_N/5*3 & _n<_N/5*4
by monthyear: replace convex_five=5 if _n>=_N/5*4

gen size_rank=1
replace size_rank=2 if nyse_decile>3
replace size_rank=3 if nyse_decile>7

replace futret=futret*100
replace futexcess=futexcess*100

egen convex_size=group(size_rank convex_five)

egen vw_raw=wtmean(futret), weight(mktcap) by(monthyear convex_size)
egen vw_excess=wtmean(futexcess),  weight(mktcap) by(monthyear convex_size)

*consolidate to one observation per month
sort convex_size monthyear 
collapse vw_raw vw_excess, by(convex_size monthyear)

tsset convex_size monthyear  

sort monthyear convex_size

keep if convex_size==1 | convex_size==5 | convex_size==6 | convex_size==10 | convex_size==11 | convex_size==15

sort monthyear convex_size

gen vw_excess_dif=vw_excess-vw_excess[_n+1] if convex_size==1 | convex_size==6 | convex_size==11

keep if convex_size==1 | convex_size==6 | convex_size==11
sort convex_size monthyear
tsset convex_size monthyear  


egen std_vw=sd(vw_excess_dif), by(convex_size)

egen mean_ret_vw=mean(vw_excess_dif) , by(convex_size)

gen sharpe_vw=mean_ret_vw/std_vw 

egen skew_ret_vw=skew(vw_excess_dif), by(convex_size)

egen kurt_ret_vw=kurt(vw_excess_dif), by(convex_size)
}

foreach i in 1 6 11 {
newey vw_excess_dif  if convex_size ==`i', lag(6)
}

keep convex_size std sharpe skew kurt

duplicates drop

foreach i in 1 6 11 {
	foreach j in std sharpe skew kurt {
		sum `j' if convex_size==`i'
	}
}
